package dhadi.model;

import java.util.Collection;

import org.json.JSONException;
import org.json.JSONObject;

import dhadi.util.Logger;

public class Pick
{
	public enum STATE 
	{
		UNUSED, ONBOARD, DESTROYED;
	};
	
	private STATE state;
	private Player owner;
	private Node node;
	private Board board;
	private boolean isLastMoved = false;
	private String name;
	public Pick(String name, Player player, Board board)
	{
	    this.name = name;
		this.owner = player;
		this.board = board;
		this.state = STATE.UNUSED;				
	}

	Pick() {
    }

    public STATE getState() {
		return state;
	}

	public void setState(STATE state) {
		this.state = state;
	}
	
	public Player getOwner() {
		return owner;
	}
	
	public Node getNode()
	{
		return this.node;
	}
	
	public void setNode(Node node) {
		if (this.node != null && this.node.equals(node)) {
			Logger.log(this.getClass(),
					"node is same cannot set same node again!");
			return;
		}
		this.releaseNode();
		this.node = node;
		this.node.placePick(this);
		this.setState(STATE.ONBOARD);
	}
	
	public void releaseNode()
	{
		if (this.node != null) {
			this.node.releasePick(this);
			this.node = null;
		}
	}
	
	/*public boolean moveToNode(Node node)
	{
		if (node != null && !node.equals(this.node))
		{
			this.board.registerMove(
					this, this.getOwner(), 
					this.node, node, 
					this.state, STATE.ONBOARD);
			this.releaseNode();
			this.node = node;
			this.node.pick = this;
			this.setState(STATE.ONBOARD);
			return true;
		}
		else
			return false;
	}*/
	
	public void destroy()
	{
		this.releaseNode();
		this.setState(STATE.DESTROYED);
	}

	public boolean isUnused() {
		return STATE.UNUSED.equals(this.getState());
	}

	public boolean isOnboard() {
		return STATE.ONBOARD.equals(this.getState());
	}

	public boolean isDestroyed() {
		return STATE.DESTROYED.equals(this.getState());
	}

	@Override
	public String toString() {
		return "Pick [board=" + board + ", node=" + (node==null?"":node.name) + ", owner=" + owner
				+ ", state=" + state + "]";
	}

	public JSONObject toJson() throws JSONException {
		JSONObject json = new JSONObject();
		json.put("Name", this.name);
		json.put("State", this.state.toString());
		json.put("Node", this.node==null?"null":this.node.getName());
		json.put("isLastMoved", this.isLastMoved);
		return json;
	}
	
    public static Pick fromJson(String jsonString, SerializationContext context) throws JSONException {
        JSONObject json = new JSONObject(jsonString);
        Pick pick = context.board.findPickByName(json.getString("Name"));
        if (context.isLoadingMoves) {
            pick.node = null;
        } else {
            throw new IllegalArgumentException("unsupported context, Could not set node to a pick");
        }
        return pick;
    }
	
	public void setLastMovedFlag(boolean flag) {
		this.isLastMoved = flag;
	}

	public boolean isLastMoved() {
		return this.isLastMoved;
	}

    public Object getName() {
        return this.name;
    }
}